package com.GatherFour;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class WiFiDirectScanner extends WifiDirectManager implements OnItemClickListener, PeerListListener{

	 private ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
	 private ListView list;
	 private TextView emptyText;
	 private WifiP2pDeviceList peerList;
	 private WifiP2pConfig config;
	 @Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.scanner);
		
		intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
		
		
		list = (ListView) findViewById(R.id.serverListID);  
		emptyText = (TextView) findViewById(R.id.emptyMsgID);
		manager.discoverPeers(channel, null);
		
		context = this.getApplicationContext();
	}
	
	@Override
	public void updateThisDevice(WifiP2pDevice device) {
		// TODO Auto-generated method stub
		if(device == null){
			return;
		}
		setDeviceStatus(device, Player.CLIENT);

        
        TextView view = (TextView) findViewById(R.id.my_name);
        view.setText(device.deviceName);
        view = (TextView) findViewById(R.id.my_status);
        view.setText(WifiDirectManager.getDeviceStatus(device.status));
        
	}

	@Override
	public void onPeersAvailable(WifiP2pDeviceList peerList) {
		// TODO Auto-generated method stub
		if(peerList == null){
			emptyText.setVisibility(View.VISIBLE);
			return;
		}
		this.peerList = peerList;
		
		emptyText.setVisibility(View.INVISIBLE);
		WifiP2pDevice device;
		Iterator<WifiP2pDevice> it = peerList.getDeviceList().iterator();
		while(it.hasNext()) {  
			device = it.next();
				HashMap<String, String> map = new HashMap<String, String>(); 
				map.put("ServerName", device.deviceName);  
				map.put("ServerStatus", WifiDirectManager.getDeviceStatus(device.status));  
				listItem.add(map);  
			
	    }  
		
		
		 SimpleAdapter listItemAdapter = new SimpleAdapter(
				this,
				listItem, 
				R.layout.list_item_view,
				new String[] {"ServerName","ServerStatus"},   
				new int[] {R.id.serverNameID,R.id.serverStatusID}  
		        );  
		 
		 list.setAdapter(listItemAdapter);
		 list.setOnItemClickListener(this);
		 
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		//manager.requestGroupInfo(channel, this);
		config = new WifiP2pConfig();
		config.deviceAddress = getDevice(arg2).deviceAddress;
		config.wps.setup = WpsInfo.PBC; 
		manager.connect(channel, config, null);
		
	}

	@Override
	public void onConnectionInfoAvailable(WifiP2pInfo info) {
		// TODO Auto-generated method stub
		this.info = info;
		TextView view = (TextView) findViewById(R.id.my_name);
        view.setText(info.groupOwnerAddress.getHostAddress());
        view = (TextView) findViewById(R.id.my_status);
        view.setText(info.isGroupOwner?"True":"False");
        
        if(info.isGroupOwner){
	      groupOwnerSetup("client");
        }else{
        	try {
				Thread.sleep(500);
				
				connectGroupOwner(info.groupOwnerAddress,"client");
				} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
        }
     

	}

	
	
	public WifiP2pDevice getDevice(int index){
		Iterator<WifiP2pDevice> it = peerList.getDeviceList().iterator();
		for(int i = 0; i< index-1; i++){
			it.next();
		}
		return it.next();
		
	}
	
	public Runnable updateView = new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			TextView view =(TextView) findViewById(R.id.my_status);
			view.setText("Received");
		}
		
	};

}

